const fs = require('fs')

const path = require('path')

const acorn = require('acorn')

async function startWork() {
  const fileBuffer = await fs.promises.readFile(path.resolve(__dirname, '../src/index.js'))

  const fileString = fileBuffer.toString()

  console.log(fileString)

  // replace无法达到期望
  // fs.promises.writeFile(path.resolve(__dirname, './main.js'), fileString.replace('import', "webpack_require"))


  // ast抽象语法分析
  const result = acorn.parse(fileString, { ecmaVersion: "2020", sourceType: 'module' })

  // console.log(result.body)

  const importStr = fileString.substring(0, 42)

  console.log(importStr)

  // 命令行参数获取 node webpack/index.js --mode=development --devtool

  console.log("process.env: ", process.argv)

  const [fst, sec, ...argsFromUser] = process.argv

  console.log(argsFromUser)

  const argList = argsFromUser.map(arg => {
    if(arg.indexOf('=') > -1) {
      const [key, value] = arg.split('=')
      return {
        key, value
      }
    } else {
      return {
        key: arg,
        value: true
      }
    }
  })

  console.log(argList)
}


startWork()